# Copyright (c) 2022, hzmes and contributors
# For license information, please see license.txt

import frappe
from frappe.model.document import Document
from frappe.model.mapper import get_mapped_doc
from frappe.permissions import add_user_permission
from frappe.utils import add_years, cstr, getdate, today, validate_email_address

class SRMSupplier(Document):
	def autoname(self):
		if self.supplier_number:
			self.name = self.supplier_number
		elif self.tax_identify:
			self.name = self.tax_identify.strip()
		else:
			pass

	def validate(self):
		self.validate_email()

	def validate_email(self):
		if self.contact:
			for row in self.contact:
				validate_email_address(row.email_address, True)
				if row.email_address != validate_email_address(row.email_address, True):
					frappe.throw(f"请确认联系人的邮箱{row.email_address}是否正确")
		if self.contactbackgroud:
			for row in self.contactbackgroud:
				validate_email_address(row.email_address, True)
				if row.email_address != validate_email_address(row.email_address, True):
					frappe.throw(f"请确认联系人的邮箱{row.email_address}是否正确")

	# 针对同一个帐户关联多个供应商档案: 增加数据权限
	@frappe.whitelist()
	def add_user_permission_supplier(self, user_name):
		add_user_permission('SRM Supplier', self.name, user_name, ignore_permissions=True)

	@frappe.whitelist()
	def append_basic_date_pdf(self):
		# 先清空子表
		self.basic_date_pdf = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Basic Date PDF', 'company_nature': self.company_nature},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('basic_date_pdf', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()

	@frappe.whitelist()
	def append_certificate(self):
		# 先清空子表
		self.certificate = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Certificate PDF'},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('certificate', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()

	@frappe.whitelist()
	def append_certificate_3c_pdf(self):
		# 先清空子表
		self.certificate_3c_pdf = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Certificate 3C PDF'},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('certificate_3c_pdf', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()

	@frappe.whitelist()
	def append_investigate(self):
		# 先清空子表
		self.investigate = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Investigate PDF'},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('investigate', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()

	@frappe.whitelist()
	def append_contract_agreement(self):
		# 先清空子表
		self.contract_agreement = []
		projects = frappe.db.get_list('SRM Supplier PDF Config', filters={'category': 'Contract And Agreement'},fields=['project', 'expiration_date'], order_by='creation', ignore_permissions=True)
		for item in projects:
			self.append('contract_agreement', {'project': item.project, 'expiration_date_required': item.expiration_date})
		self.save()


# 生成[转三级审批]单
@frappe.whitelist()
def make_supplier_approval(source_name, target_doc=None):
	doc = get_mapped_doc(
		"SRM Supplier",
		source_name,
		{
			"SRM Supplier": {
				"doctype": "SRM Supplier Approval L2L3",
				"field_map": {"name": "supplier_number"},
				"validation": {
					"managestatus": ["=", "二级"],
				},
			},
			# "postprocess": update_item,
			# "condition": lambda doc: abs(doc.received_qty) < abs(doc.qty),
		},
		target_doc,
	)
	doc.insert()
	return doc
		